package com.bdqn.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.bdqn.entity.Employee;
import com.bdqn.entity.Module;
import com.bdqn.entity.Role;
import com.bdqn.mapper.ModuleMapper;
import com.bdqn.mapper.RoleMapper;
import com.bdqn.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/sys/users")
public class UserController {
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private ModuleMapper moduleMapper;
	@Autowired
	private RoleMapper roleMapper;

	/**
	 * 处理业务逻辑的方法 RequestMapping：请求的url
	 */
	@RequestMapping("/login")
	public String doLogin(Employee user, HttpSession session) {

		Employee us = userMapper.login(user);
		System.out.println(us);
		if (us != null) {
			System.out.println("登录成功！！" );
			// 查询菜单

			List<Module> lisModule = moduleMapper.getAllModuleByRoleId(us.getRoleId());
			session.setAttribute("mlist", lisModule);

			List<Role> listRole = roleMapper.getAllRole();
			session.setAttribute("listRole", listRole);

			session.setAttribute("user", us);
			session.setAttribute("emp", us);
			return "redirect:/index.jsp"; // 默认转发
		} else {
			System.out.println("登录失败！！");
			return "redirect:/login.jsp";
		}

	}

	@RequestMapping("/logout")
	public String logout(HttpSession session) {
		session.invalidate();
		return "redirect:/login.jsp";

	}

	@RequestMapping("/findAllEmp")
	public String findAll(HttpSession session, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo) {
        
		PageHelper.startPage(pageNo, 10);

		List<Employee> listEmp = userMapper.getAllEmp();
		
		PageInfo<Employee> pageInfo = new PageInfo<>(listEmp);
		
		session.setAttribute("pageInfo", pageInfo);
		return "redirect:/jsp/sys/users/userList.jsp";

	}
//	public String findAll(Model model, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo) {
//      
//		PageHelper.startPage(pageNo, 10);
//
//		List<Employee> listEmp = userMapper.getAllEmp();
//		
//		PageInfo<Employee> pageInfo = new PageInfo<>(listEmp);
//		
//		model.addAttribute("pageInfo", pageInfo);
//		return "forward:/jsp/sys/users/userList.jsp";
//
//	}
	

	@RequestMapping("/doUpateEmp")
	public String userAdd(Employee user) {
		System.out.println("执行添加操作");
		int num = userMapper.save(user);
		if (num > 0) {
			System.out.println("添加成功");
			return "redirect:/jsp/sys/users/findAllEmp";
		} else {
			System.out.println("添加失败");
			return "redirect:/jsp/sys/users/userAdd.jsp";

		}

	}

	@RequestMapping("/delEmp")
	public String delEmp(int id) {
		System.out.println("删除" + id);
		int num = userMapper.delEmp(id);
		System.out.println(num);
		return "redirect:/jsp/sys/users/findAllEmp";

		
	}
	
	@RequestMapping("/updateEmp")
	public String updateEmp(int id,Model model) {
		Employee employee = userMapper.searchEmpById(id);
		
		model.addAttribute("employee",employee);//reques
		
		return "forward:/jsp/sys/users/usermodify.jsp";
		
	}
	@RequestMapping("/addEmp")
	public String doUpateEmp(Employee user) {
		
	int num =	userMapper.updateEmp(user);
		
	System.out.println(num>0?"修改成功":"修改失败");
	
		return "redirect:sys/users/findAllEmp";
		
	}
	
	//查看用户信息
	@RequestMapping("/userView")
		
		public String userView(int id,Model model) {
		Employee emp = userMapper.searchEmpById(id);
		model.addAttribute("emp",emp);
		
		return "forward:/jsp/sys/users/userView.jsp";
		
	}
	
	
	
	
	
	
}
